<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forgot_password extends CI_Controller {

	public function __construct()
    {
    	parent::__construct();
		if(!isset($_SESSION)): session_start(); endif;
		$this->load->model('users_model');
		$this->load->helper('myuser_helper');
    }

	public function index(){
		$data['title']= "My Life My Eulogy | Forgot Password";
		$data['main_content'] = 'forgot_password_view';
		$data['message'] = '';
		$data['email'] = '';
		if(isset($_POST['btn-forgot'])){
			$email = $this->input->post('fp-email');
			$valid = $this->users_model->check_exist_email($email);
			if($valid){
				$new_pass = Myuser_helper::randLetterNumber();
				$update = $this->users_model->update_user_password($email,$new_pass);
				if($update){
					$send_to = $email;
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= "From: My Life My Eulogy <noreply@myeulogy.com> \r\n";
					$subject = "My Life My Eulogy - New Password";
					$body  = 'Your new password is: '.$new_pass.'<br/>';
					$body .= 'Clink the link below to access the My Life My Eulogy'.'<br/>';
					$url = base_url()."login";
					$body .= "<a href='$url'>$url</a>";
					mail($send_to, $subject, $body,$headers);
					$data['message'] = 'Successfully sent new password.';
				}//end if
			}else{
				$data['message'] = 'Email doesn\'t exist.';
			}
			$data['email'] = $email;
		}//end if
		$this->load->view('common/temp', $data);
	}
}